package com.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.admin.dao.ApaymentDao;
import com.member.domain.MemberCommand;
import com.payment.domain.PaymentCommand;
import com.point.domain.PointCommand;

@Controller
public class ApaymentUpdateController {	
	@Autowired
	private ApaymentDao apaymentDao;

	@RequestMapping("/admin/stateUpdate.do")
	public String update(@RequestParam("num")int num, @RequestParam("pageNum")int pageNum){

		PaymentCommand paymentCommand = apaymentDao.selectPayment(num);
		PointCommand pointCommand = new PointCommand();	
		MemberCommand memberCommand = new MemberCommand(); 

		try{

			if(paymentCommand.getState() != 1){
				paymentCommand.setState(1);
				pointCommand.setId(paymentCommand.getId());
				pointCommand.setnPoint(paymentCommand.getAmount());
				pointCommand.setMinusPoint(0);
				memberCommand.setId(paymentCommand.getId());

				if(apaymentDao.selectPointSum(pointCommand) != 0){
					int sumPoint = apaymentDao.selectPointSum(pointCommand);
					System.out.println("2");
					memberCommand.setNpoint(paymentCommand.getAmount() + sumPoint);
				}
			}
		}catch(NullPointerException e){
			memberCommand.setNpoint(paymentCommand.getAmount());
			System.out.println("3");
		}

		apaymentDao.insertPoint(pointCommand);
		apaymentDao.update(paymentCommand);
		apaymentDao.memberPointUpdate(memberCommand);
		return "redirect:/admin/paymentList.do?pageNum="+pageNum;
	}
}
